package com.xix.canal.client.writer.mysql.config;

import com.xixi.canal.client.support.ConfigProperties;
import com.xixi.canal.client.util.StringUtil;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@ConfigProperties(prefix = "canal.mysql")
public class MysqlConfig {

    private DataSourceInfo dataSource;

    private Integer batchSize = 3000;

    private List<String> ignoreField;

    private boolean skipDupException = true;

    private TargetConfig target;

    public DataSourceInfo getDataSource() {
        return dataSource;
    }

    public List<String> getIgnoreField() {
        return ignoreField;
    }

    public void setIgnoreField(List<String> ignoreField) {
        this.ignoreField = ignoreField;
    }

    public boolean getSkipDupException() {
        return skipDupException;
    }

    public void setSkipDupException(boolean skipDupException) {
        this.skipDupException = skipDupException;
    }

    public void setDataSource(DataSourceInfo dataSource) {
        this.dataSource = dataSource;
    }

    public Integer getBatchSize() {
        return batchSize;
    }

    public void setBatchSize(Integer batchSize) {
        this.batchSize = batchSize;
    }

    public TargetConfig getTarget() {
        return target;
    }

    public void setTarget(TargetConfig target) {
        this.target = target;
    }

    public String getDataSourceKey() {
        if (null == dataSource || null == dataSource.getUrl()) return null;
        String url = dataSource.getUrl();
        int index = url.indexOf("?");
        if (index >= 0) {
            url = url.substring(0, index);
        }
        return url.toUpperCase();
    }


    public static class DataSourceInfo {

        private String driverClassName;
        private String url;
        private String username;
        private String password;

        public String getDriverClassName() {
            return driverClassName;
        }

        public void setDriverClassName(String driverClassName) {
            this.driverClassName = driverClassName;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }

    public static class TargetConfig {

        private String sourceTable;
        private String targetTable;
        private Map<String, String> pkColumns;
        private Boolean autoMap;
        private Boolean updated;
        private String resultType;
        private Map<String, String> columns;
        private Map<String, String> allColumns;

        public String getSourceTable() {
            return sourceTable;
        }

        public String getSourceDB() {
            String sourceDb = targetTable == null ? "" : targetTable;
            int index = sourceDb.indexOf(".");
            if (index >=0) {
                sourceDb = sourceDb.substring(0, index);
            }

            return sourceDb;
        }

        public void setSourceTable(String sourceTable) {
            this.sourceTable = sourceTable;
        }

        public String getTargetTable() {
            return targetTable;
        }

        public void setTargetTable(String targetTable) {
            this.targetTable = targetTable;
        }

        public Map<String, String> getPkColumns() {
            return pkColumns;
        }

        public void setPkColumns(String pkColumns) {
            if (this.pkColumns == null) this.pkColumns = new LinkedHashMap<>();

            this.pkColumns.putAll(StringUtil.stringToMap(pkColumns));
        }

        public Boolean getAutoMap() {
            return autoMap;
        }

        public void setAutoMap(Boolean autoMap) {
            this.autoMap = autoMap;
        }

        public String getResultType() {
            return resultType;
        }

        public void setResultType(String resultType) {
            this.resultType = resultType;
        }

        public Map<String, String> getColumns() {
            return columns;
        }

        public void setColumns(String columns) {

            if (this.columns == null) this.columns = new LinkedHashMap<>();

            this.columns.putAll(StringUtil.stringToMap(columns));
        }

        public Boolean getUpdated() {
            return updated;
        }

        public void setUpdated(Boolean updated) {
            this.updated = updated;
        }

        public Map<String, String> getAllColumns() {
            return allColumns;
        }

        public void setAllColumns(Map<String, String> allColumns) {
            this.allColumns = allColumns;
        }
    }
}
